﻿using Domain;
using Domain.Entity;
using Domain.ValueObject;
using Infrastructure;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using WebAPI.Filter;

namespace WebAPI.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [UnitOfWork(typeof(UserContext))]
    public class UserManagerController : ControllerBase
    {
        private readonly UserDomainService domainService;
        private readonly IUserDomainRepository repository;
        private readonly UserContext dbContext;

        public UserManagerController(UserDomainService domainService, IUserDomainRepository repository,UserContext userContext)
        {
            this.domainService = domainService;
            this.repository = repository;
            this.dbContext = userContext;

        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="phone"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPost]
        public async Task<IActionResult> Login(PhoneNumber phone, string pwd)
        {
            UserAccessResult result = await domainService.CheckLoginAsync(phone, pwd);
            switch (result)
            {
                case UserAccessResult.OK:
                    return Ok("登录成功");
                case UserAccessResult.PhoneNumberNotFound:
                    return BadRequest("手机号或者密码错误");//避免泄密,不能404
                case UserAccessResult.Lockout:
                    return BadRequest("用户被锁定，请稍后再试");
                case UserAccessResult.NoPassword:
                case UserAccessResult.PasswordError:
                    return BadRequest("手机号或者密码错误");
                default:
                    throw new NotImplementedException();
            }
        }

        /// <summary>
        /// 添加用户信息
        /// </summary>
        /// <param name="phone"></param>
        /// <returns></returns>
        [HttpPost]     
        public async Task<IActionResult> AddUserInfo(PhoneNumber phone) 
        {
            if (null != await repository.FindOneAsync(phone))
            {
                return BadRequest("手机号已经存在");
            }
            User user = new User(phone);
            dbContext.Users.Add(user);
            return Ok("成功");           
        }

        /// <summary>
        /// 更改密码
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        [HttpPut]      
        public async Task<IActionResult> ChangePWD(Guid userId, string pwd)
        {
            User? user = await repository.FindOneAsync(userId);
            if (null == user) return NotFound();
            user.ChangePassword(pwd);
            return Ok("成功");
        
        }

        /// <summary>
        /// 解除锁定
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPut]
        [Route("{id}")]
        public async Task<IActionResult> Unlock(Guid id)
        {
            var user = await repository.FindOneAsync(id);
            if (user == null)
            {
                return NotFound();
            }
            domainService.ResetAccessFail(user);
            return Ok("成功");
        }

        /// <summary>
        /// 获取所有用户信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetAll()
        {
            var users = await dbContext.Users.ToListAsync();
            return Ok(users);
        }

        //[HttpPost]
        //public async Task<IActionResult> AddUserLoginHistory(PhoneNumber phone) 
        //{
        //    UserLoginHistory history = new UserLoginHistory(null, phone, "自己创建的-测试");
        //    dbContext.UserLoginHistories.Add(history);
        //    await dbContext.SaveChangesAsync();
        //    return Ok("成功");
        //}
    }
}
